package top.withwings.bonus.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import top.withwings.bonus.common.service.UserService;

@EnableWebSecurity
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Autowired

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());

    }

    @Override
    public void configure(WebSecurity web) throws Exception {

        web.ignoring().antMatchers("/css/**","/fonts/**","/images/**","/i/**","/js/**");

    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable().cors()
                .and()
                .authorizeRequests().anyRequest().permitAll();
//                .mvcMatchers(HttpMethod.GET,"/**").permitAll() //请求View页面 用GET请求，全部允许
//                .mvcMatchers(HttpMethod.OPTIONS,"/**").permitAll()//预检请求OPTIONS方法，全部允许
//                .mvcMatchers("/token").permitAll()    //获取token，全部允许
//                .mvcMatchers(HttpMethod.POST,"/revokeToken","/userInfo","/user/updatePassword").authenticated()//用户注销，获取用户信息，更新密码，所有角色允许
//                .mvcMatchers(HttpMethod.POST,"/final/history","/bonusForm/preview")
//                .hasAnyRole("ADMIN","SENIOR_SALEMAN","MIDDLE_SALEMAN","LOW_SALEMAN")    //总提成历史，单项提成预览 全部允许 在controller内部判断权限
//
//                .anyRequest().hasRole("ADMIN") //其他请求，全部需要登陆
//                .and()
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
//                .and()
//                .httpBasic().disable()
//                .formLogin().disable()
//                .addFilterBefore(new YiyuanAuthenticationFilter(tokenService),BasicAuthenticationFilter.class);

    }


}